package com.bcu.calculus.controller;

import com.bcu.calculus.config.GlobalConfig;
import com.bcu.calculus.entity.Message;
import com.bcu.calculus.service.ExcelService;
import com.bcu.calculus.service.UserInfoService;
import com.bcu.calculus.service.UserService;
import com.bcu.calculus.utils.FileUtil;
import com.bcu.calculus.utils.ZipUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.*;


@Api(tags = "07  文件导出模块")
@RestController
@RequestMapping("/api/File")
public class ExcelController {

    @Resource
    private ExcelService excelService;

    @Resource
    private GlobalConfig globalConfig;

    @Resource
    private UserService userService;


    @ApiOperation(value = "导出成绩结果Excel,返回url")
    @PostMapping("/exportExcel")
    public Message  getExcel(Integer semId) throws Exception {
        return excelService.getExcel(semId);
        }


    @ApiOperation(value = "获取当前学期的压缩文件通过学期名" )
    @GetMapping(value = "/getZip")
    public Message getZipFileBySemName(String SemName) throws FileNotFoundException {

        //zip文件的位置
        String zipfile = globalConfig.filePath+"image/userSendImage/"+"zipfile/"+SemName+".zip";

        //zip文件的位置(本地测试)
        //String zipfile = "E:/test/"+SemName+".zip";
        //需要压缩的文件目录
        String file = globalConfig.filePath+"image/userSendImage/"+SemName;

        File file1 = new File(file);
        if (!file1.exists()) {
            return Message.fail("该文件不存在");
        }

        //判断是否已经存在压缩文件，如果有删除掉
        File file2 = new File(zipfile);
        if (file2.exists()) {
            file2.delete();
        }

        FileOutputStream fos1 = new FileOutputStream(new File(zipfile));

        ZipUtil.toZip(file, fos1, true);

        return Message.success(globalConfig.hostUrl+"bcucalculusFile/image/userSendImage/zipfile/"+SemName+".zip");

    }

    @ApiOperation(value = "获取当前学期的压缩文件通过学期名以及路由名字")
    @GetMapping(value = "/getZipBySemNameAndRouterName")
    public Message getZipFileBySemNameAndRouterName(String SemName,String routerName) throws FileNotFoundException {

        //zip文件的位置
        String zipfile = globalConfig.filePath+"image/userSendImage/"+"zipfile/"+SemName+"-"+routerName+".zip";
        //System.out.print(zipfile);

        //System.out.print(globalConfig.hostUrl+"bcucalculusImage/zipfile/"+SemName+"-"+routerName+".zip");


        //zip文件的位置(本地测试)
        //String zipfile = "E:/test/"+SemName+".zip";

        //需要压缩的文件目录
        String file = globalConfig.filePath+"image/userSendImage/"+SemName+"/"+routerName;
        //System.out.print(file);


        File file1 = new File(file);
        if (!file1.exists()) {
            return Message.fail("该文件不存在");
        }

        //判断是否已经存在压缩文件，如果有删除掉
        File file2 = new File(zipfile);
        if (file2.exists()) {
            file2.delete();
        }

        FileOutputStream fos1 = new FileOutputStream(new File(zipfile));

        ZipUtil.toZip(file, fos1, true);

        return Message.success(globalConfig.hostUrl+"bcucalculusFile/image/userSendImage/zipfile/"+SemName+"-"+routerName+".zip");

    }


    @ApiOperation(value = "删除文件")
    @GetMapping(value = "/delectFile")
    public Message delectFile(String SemId,String routerId,String userId,String actId,String file){

        String file1 = SemId+"/"+routerId+"/"+userId+"/"+actId+"/"+file; //目录
        String path = globalConfig.filePath+"image/userSendImage/"+file1;
        System.out.print(path);
        File file2 = new File(path);
        if (file2.exists()) {
            file2.delete();
            return Message.success("删除"+path+"成功");
        }else {
            return Message.fail(path+"不存在");
        }


    }

    @ApiOperation(value = "删除目录（需要按照数字顺序填写目录，只能删除存储该项目下的bcucalculusImage目录中的内容）")
    @GetMapping(value = "/delectFiles")
    public Message delectFiles(String SemId1,String routerId2,String userId3,String actId4){

        String file1 = null;
        //SemId+"/"+routerId+"/"+userId+"/"+actId+"/"+file; //目录


        if (SemId1 == null){
            return Message.fail("学期项为空");
        }else {
            if (routerId2 == null){
                file1 = SemId1;
            }else {
                if (userId3 == null){
                    file1 = SemId1+"/"+routerId2;
                }else {
                    if (actId4 == null){
                        file1 = SemId1+"/"+routerId2+"/"+userId3;
                    }else {
                        file1 = SemId1+"/"+routerId2+"/"+userId3+"/"+actId4;
                    }

                }
            }
        }



        String path = globalConfig.filePath+"image/userSendImage/"+file1;
        System.out.print(path);
        File file2 = new File(path);
        if (file2.exists()) {
            FileUtil.deleteDir(file2);
            return Message.success("删除文件夹："+path+" 成功");
        }else {
            return Message.fail(path+"不存在");
        }


    }

    @ApiOperation(value = "上传Excel文件添加和更新用户")
    @PostMapping(value = "/uploadbyExcel", headers = "content-type=multipart/form-data")
    public Message UpLoadDateByExcel(@RequestParam(value = "file") MultipartFile excel, HttpServletRequest request) throws IOException {
        InputStream inputStream = null;
        try{
            inputStream = excel.getInputStream();
        }catch (Exception e){
            return Message.fail("读取文件失败");
        }
        return userService.uploadByExcel(inputStream);
    }

}
